Scroll Progress Bar

Dynamic Memory allocation

Dynamic memory allocation in C allows allocating memory for variables at runtime, rather than at compile time. This is achieved using four library functions: malloc, calloc, realloc, and free, which are defined in the stdlib.h header file.

malloc: Allocates a block of memory of a specified size and returns a pointer to the first byte of the allocated memory.

calloc: Allocates memory for an array of elements of a specified size and initializes all bytes to zero.

realloc: Changes the size of the previously allocated memory block.

free: Frees the memory previously allocated by malloc, calloc, or realloc, releasing it back to the system.

Usage and Sample Code:

#include <stdio.h>
#include <stdlib.h>

int main() {
    // Dynamic memory allocation for an array of integers
    int size;
    printf("Enter the size of the array: ");
    scanf("%d", &size);

    int *dynamicArray = (int *)malloc(size * sizeof(int));

    // Check if memory allocation was successful
    if (dynamicArray == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    // Input array elements
    printf("Enter %d elements:\n", size);
    for (int i = 0; i < size; i++) {
        scanf("%d", &dynamicArray[i]);
    }

    // Output array elements
    printf("Array Elements: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", dynamicArray[i]);
    }
    printf("\n");

    // Free the allocated memory
    free(dynamicArray);

    return 0;
}
Output:

Enter the size of the array: 5
Enter 5 elements:
10 20 30 40 50
Array Elements: 10 20 30 40 50
Explanation:
  • In the sample code, first take input from the user to determine the size of the dynamic array (number of elements).
  • Then use the malloc function to allocate memory for the array. The size of the array is determined by size * sizeof(int), where size is the user-provided value for the number of elements, and sizeof(int) is the size of each integer element in bytes.
  • Check if the memory allocation was successful by verifying if the pointer dynamicArray is not NULL.
  • Next, take input from the user for the elements of the dynamic array.
  • Then print the elements of the dynamic array using a loop.
  • Finally, use the free function to release the memory previously allocated for the dynamic array. It is essential to free dynamically allocated memory to avoid memory leaks.
  • Remember that when use dynamic memory allocation, it is r responsibility to free the allocated memory using free once no longer need it. Otherwise, it can lead to memory leaks and consume unnecessary memory. Dynamic memory allocation is particularly useful when the size of the array or the amount of memory needed is not known at compile time.

What is malloc used for in C?


Memory

What does calloc initialize memory to in C?


Zero

What is the purpose of realloc in C?


Resize

What function is used to release memory in C?


Free

What is the return type of malloc in C?


Pointer